package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    @GetMapping("list")
    public PageDTO<Item> list(@RequestParam("page") int page,@RequestParam("size") int size){
        return itemService.listByPage(page,size);
    }

    @PostMapping
    public void add(@RequestBody Item item){
        itemService.add(item);
    }
    @GetMapping("id")
    public Item getById(@PathVariable("id") Long id){
        return itemService.getById(id);
    }

    @PutMapping
    public void updateById(@RequestBody Item item){
        itemService.updateItem(item);
    }

    @PutMapping("/status{id}/{status}")
    public void updateStatus(@PathVariable("id") Long id,@PathVariable("status") Integer status){
       //update item set status = ？ where id = ？
        LambdaUpdateWrapper<Item> query = new LambdaUpdateWrapper<>();
        query.eq(Item::getId,id);
        query.set(Item::getId,id);
        query.set(Item::getUpdateTime,new Date());//???.sql
    }

    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable("id") Long id){
        itemService.deleteItem(id);
    }



}
